package com.shemy.interceptor;

import com.shemy.entity.SysMenu;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description 自定义拦截器
 * @Author Dzsom
 * @Date 2019-05-07 15:12
 * @Encoding UTF-8
 * @Version 1.0
 **/
public class MyHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        System.out.println(requestURI);
        List<SysMenu> sysMenus= (List<SysMenu>) request.getSession().getAttribute("menus");
        for (SysMenu sysMenu : sysMenus) {
            if (requestURI.contains(sysMenu.getUrl())){
                return true;
            }
        }
        response.sendRedirect("/unauthorized");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
